# Partisan Collaboration in Policy Adoption: 
# An Experimental Study with Local Government Officials
# Yixin Liu 2024
# Policy Studies Journal
# Replication file for hypotheses testing

####Packages####
library(ggplot2)
library(egg)
library(cregg)
library(ggpubr)
library(cowplot)
library(Hmisc)
library(patchwork)
library(texreg)

####Functions####
element_textbox <- function(...) {
  el <- element_text(...)
  class(el) <- c("element_textbox", class(el))
  el
}

element_grob.element_textbox <- function(element, ...) {
  text_grob <- NextMethod()
  rect_grob <- element_grob(calc_element("strip.background", theme_bw()))
  
  ggplot2:::absoluteGrob(
    grid::gList(
      element_grob(calc_element("strip.background", theme_bw())),
      text_grob
    ),
    height = grid::grobHeight(text_grob), 
    width = grid::unit(1, "npc")
  )
}

####Data####
# Setup working directory
WD = getwd()
setwd(WD)

df<-read.csv("conjoint.csv")

####Attribute####
# factor and label
# resource allocation
df$resource=as.factor(df$resource)
label(df$resource) <- "Self vs Partner's Input"

# Policy outcome
df$outcome=as.factor(df$outcome)
label(df$outcome) <- "Job Creation"

# co-partisanship
df$partyid=as.factor(df$partisanship)
df$sameparty<-ifelse(df$partyid=="Republicans" & df$party==2,1,
                     ifelse(df$partyid=="Democrats" & df$party==1,1,0))
df$sameparty<-ifelse(df$sameparty==1,"Same Party","Different Party")
df$sameparty=as.factor(df$sameparty)
label(df$sameparty) <- "Program Proposed By"

# trust
df$trust=as.factor(df$trust)
df$trust<-relevel(df$trust, "No Experience")
df$trust<-relevel(df$trust, "Bad")
label(df$trust) <- "Collaborative Experiences"

# attribute feature
feature.labs <- c("Resource Allocation:\nSelf vs Partner's Input", 
                  "Policy Outcome:\nJob Creation", 
                  "Co-Partisanship:\nProgram Proposed By", 
                  "Reciprocal Trust:\nCollaborative Experiences")

names(feature.labs) <- c("Self vs Partner's Input", 
                         "Job Creation", 
                         "Program Proposed By", 
                         "Collaborative Experiences")

####Coveriates####
#####ideology####
df$ideo_text<-"Very Liberal"
df$ideo_text[df$ideo==2]<-"Liberal"
df$ideo_text[df$ideo==3]<-"Moderate"
df$ideo_text[df$ideo==4]<-"Conservative"
df$ideo_text[df$ideo==5]<-"Very Conservative"
df$ideo_text<-as.factor(df$ideo_text)
df$ideo_text <- ordered(df$ideo_text, levels = c("Very Liberal", 
                                                 "Liberal", 
                                                 "Moderate", 
                                                 "Conservative", 
                                                 "Very Conservative"))

# ideology extremism
df$ideo_intensity<-0
df$ideo_intensity[df$ideo==1]<-2
df$ideo_intensity[df$ideo==5]<-2
df$ideo_intensity[df$ideo==2]<-1
df$ideo_intensity[df$ideo==4]<-1

df$ideo_extreme[df$ideo_intensity==0] <- "Moderate"
df$ideo_extreme[df$ideo_intensity==1] <- "Weak Ideology"
df$ideo_extreme[df$ideo_intensity==2] <- "Strong Ideology"
df$ideo_extreme<-as.factor(df$ideo_extreme)

# conservatism
df$conservatism[df$ideo==1 | df$ideo==2] <- "Liberal"
df$conservatism[df$ideo==3] <- "Moderate"
df$conservatism[df$ideo==4 | df$ideo==5] <- "Conservative"
df$conservatism<-as.factor(df$conservatism)
df$conservatism<-relevel(df$conservatism, "Moderate")
df$conservatism<-relevel(df$conservatism, "Liberal")

#####elected####
df$elected<-ifelse(df$elected==1, "Elected","Appointed")
df$elected=as.factor(df$elected)
df$elected<-relevel(df$elected, "Appointed")

#####female####
df$female<-ifelse(df$gender==2,"Female","Other")
df$female<-as.factor(df$female)
df$female<-relevel(df$female, "Other")

#####race####
df$white_respond<-ifelse(df$race==1,1,0)
df$white_respond<-ifelse(df$white_respond==1, "White", "Nonwhite")
df$white_respond=as.factor(df$white_respond)

#####education####
df$grad<-ifelse(df$educ>5,1,0)
df$grad<-ifelse(df$grad==1,"Graduate Degree", "Other")
df$grad=as.factor(df$grad)
df$grad<-relevel(df$grad, "Other")

#####tenure####
df$tenure5y<-ifelse(df$tenure>2,1,0)
df$tenure5y<-ifelse(df$tenure5y==1,"More Than 5 Years","Less Than 5 Years")
df$tenure5y=as.factor(df$tenure5y)
df$tenure5y<-relevel(df$tenure5y, "Less Than 5 Years")

df$tenure<-as.factor(df$tenure)

#####age####
df$age1<-ifelse(df$age>60,1,0)
df$age1<-ifelse(df$age1==1,"Above 60","Below 60")
df$age1=as.factor(df$age1)
df$age1<-relevel(df$age1, "Below 60")

#####population####
df$pop<-ifelse(df$pop1000>100,1,0)
df$pop1<-ifelse(df$pop==1,"Above 100k","Below 100k")
df$pop1=as.factor(df$pop1)
df$pop1<-relevel(df$pop1, "Below 100k")

#####party####
df$party1<-ifelse(df$party==1, "Democrat",
                  ifelse(df$party==2, "Republican", "Other"))
df$party1=as.factor(df$party1)

# subgroup
df.party<-subset(df,df$party<3)
df.dem<-subset(df,df$party==1)
df.rep<-subset(df,df$party==2)
df.ind<-subset(df,df$party>2)

####Figure 2####
# Main Estimates of Partisanship and Collaborative Attributes
#####MM####
main.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
              id = ~ IndID, estimate = "mm")

mm.plot<-plot(main.mm, size = 3, vline = 0.5,
              feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.25,0.75), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  labs(title="Overall") +
  scale_color_manual(values = c("black","black","black","black"))+
  theme_bw()
mm.plot<-mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                       legend.position = "none",
                       strip.text = element_text(size = 14),
                       axis.text.y=element_text(size = 12),
                       axis.text.x = element_text(size = 12),
                       axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top") +
  guides(colour = guide_legend(reverse=T))
mm.plot

#####MM:Party####
group_names_party <- c("Democrat","Republican")
df.party$party1 <- droplevels(df.party$party1)

party.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
               id = ~ IndID, estimate = "mm", by=~party1)

party.mm.plot<-plot(party.mm, group="party1", size = 3, vline = 0.5,
                    feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.25,0.75), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  labs(title = "Party Subgroups") +
  scale_color_manual("party1",
                     breaks = group_names_party,
                     values = c("blue", "firebrick1"))+
  theme_bw()
party.mm.plot<-party.mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                   legend.position="right",
                                   legend.title = element_blank(),
                                   legend.text = element_text(size = 12),
                                   strip.text = element_text(size = 14),
                                   axis.text.y=element_blank(),
                                   axis.ticks.y = element_blank(),
                                   axis.text.x = element_text(size = 12),
                                   axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top") +
  guides(colour = guide_legend(reverse=T))
party.mm.plot

#####Combine####
figure2<-list(mm.plot,party.mm.plot)
figure2<-wrap_plots(figure2,ncol = 2, nrow = 1)
figure2
ggsave(file = "figure2.eps", width = 9, height = 7, dpi = 666)
ggsave(file = "figure2.jpg", width = 9, height = 7, dpi = 666)

####Figure 3####
# Conditional Effects of Partisanship on Collaborative Attributes in Program Adoption
group_names <- c("Same Party","Different Party")
#####Overall####
conditional<- cj(df.party, choice ~ resource + trust + outcome, 
                 id = ~ IndID, estimate = "mm", by = ~ sameparty)

conditional.plot<-plot(conditional, group="sameparty", size = 3, vline = 0.5,
                       feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.19,0.81), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  labs(title="Overall") +
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
conditional.plot<-conditional.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                         legend.position=c(1,1),
                                         legend.justification=c(1,1),
                                         legend.title = element_text(size = 14),
                                         legend.key = element_blank(),
                                         strip.text = element_text(size = 14),
                                         legend.text = element_text(size = 14),
                                         legend.background = element_blank(),
                                         axis.text.y=element_text(size = 12),
                                         axis.text.x = element_text(size = 12),
                                         axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.plot

#####Democrat####
conditional.d<- cj(df.dem, choice ~ resource + trust + outcome, 
                   id = ~ IndID, estimate = "mm",by = ~ sameparty)

conditional.d.plot<-plot(conditional.d, group="sameparty", size = 3, vline = 0.5,
                         feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.19,0.81), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  labs(title="Democrat Respondents") +
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
conditional.d.plot<-conditional.d.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                             legend.position=c(1,1),
                                             legend.justification=c(1,1),
                                             legend.title = element_text(size = 14),
                                             legend.key = element_blank(),
                                             strip.text = element_text(size = 14),
                                             legend.text = element_text(size = 14),
                                             legend.background = element_blank(),
                                             axis.text.x = element_text(size = 12),
                                             axis.title.x = element_text(size = 14),
                                             axis.text.y = element_blank(),
                                             axis.ticks.y = element_blank()) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.d.plot

#####Republican####
conditional.r<- cj(df.rep, choice ~ resource + trust + outcome, 
                   id = ~ IndID, estimate = "mm", by = ~ sameparty)

conditional.r.plot<-plot(conditional.r, group="sameparty", size = 3, vline = 0.5,
                         feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.19,0.81), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  labs(title="Republican Respondents") +
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
conditional.r.plot<-conditional.r.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                             legend.position=c(1,1),
                                             legend.justification=c(1,1),
                                             legend.title = element_text(size = 14),
                                             legend.key = element_blank(),
                                             strip.text = element_text(size = 14),
                                             legend.text = element_text(size = 14),
                                             legend.background = element_blank(),
                                             axis.text.x = element_text(size = 12),
                                             axis.title.x = element_text(size = 14),
                                             axis.text.y = element_blank(),
                                             axis.ticks.y = element_blank()) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.r.plot

#####Combine####
figure3<-list(conditional.plot,conditional.d.plot,conditional.r.plot)
figure3<-wrap_plots(figure3,ncol = 3, nrow = 1) +
  plot_layout(guides="collect") & theme(legend.position = 'bottom')
figure3
ggsave(file = "figure3.eps", width = 9.5, height = 7, dpi = 666)
ggsave(file = "figure3.jpg", width = 9.5, height = 7, dpi = 666)

####Figure 4####
# Co-Partisanship Effects in Ideology and Professional Experiences Subgroups
extract_marginal <- function(df) {
  if (!is.data.frame(df)) {
    stop("Input is not a data frame")
  }
  
  even_rows <- df[seq(2, nrow(df), by = 2), ]
  
  selected_columns <- even_rows[, c("BY", "estimate", "lower", "upper")]
  
  return(selected_columns)
}
#####ideology&party####
conditional.ideo<- cj(df.party, choice ~ sameparty, 
                      id = ~ IndID, estimate = "amce",
                      by = ~ conservatism)

df.conditional.ideo <- extract_marginal(conditional.ideo)
print(df.conditional.ideo)

df.conditional.ideo$BY <- factor(df.conditional.ideo$BY, 
                                 levels = c("Liberal", 
                                            "Moderate", 
                                            "Conservative"))

ideo_party_plot <- ggplot(df.conditional.ideo, aes(x=BY, y=estimate)) + 
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.1, position=position_dodge(.3),color="#9999CC") +
  geom_point(size=5, position=position_dodge(.3), color="#9999CC") +  
  geom_hline(yintercept = 0, color="#CC6666") + 
  ylim(-0.05, 0.27) +
  labs(x = element_blank(),
       y = element_blank(),
       title = "Co-Partisanship Effect\nin Conservatism Subgroups") +
  theme_bw()

ideo_party_plot <- ideo_party_plot + 
  theme(axis.text.y = element_text(size=12),
        axis.text.x = element_text(size=14),
        legend.position = "none",
        plot.title = element_textbox(
          hjust = 0.5, margin = margin(t = 5, b = 5), size = 16))

ideo_party_plot

#####elected&party####
conditional.elected<- cj(df.party, choice ~ sameparty, 
                         id = ~ IndID, estimate = "amce",
                         by = ~ elected)

df.conditional.elected <- extract_marginal(conditional.elected)
print(df.conditional.elected)

ggplot(df.conditional.elected, aes(x=BY, y=estimate)) + 
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.1, position=position_dodge(.3),color="#9999CC") +
  geom_point(size=5, position=position_dodge(.3), color="#9999CC") +  
  geom_hline(yintercept = 0, color="#CC6666") + 
  ylim(-0.05, 0.27)+
  labs(x = element_blank(),
       y = element_blank(),
       title = "Co-Partisanship Effect\nin Role Subgroups") +
  theme_bw() -> elected_party_plot
elected_party_plot + 
  theme(axis.text.y = element_text(size=12),
        axis.text.x = element_text(size=14),
        legend.position = "none",
        plot.title = element_textbox(
          hjust = 0.5, margin = margin(t = 5, b = 5), size = 16)) -> elected_party_plot

elected_party_plot

#####tenure&party####
conditional.tenure<- cj(df.party, choice ~ sameparty, 
                        id = ~ IndID, estimate = "amce",
                        by = ~ tenure)

df.conditional.tenure <- extract_marginal(conditional.tenure)
print(df.conditional.tenure)

ggplot(df.conditional.tenure, aes(x=BY, y=estimate)) + 
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.1, position=position_dodge(.3),color="#9999CC") +
  geom_point(size=5, position=position_dodge(.3), color="#9999CC") +  
  geom_hline(yintercept = 0, color="#CC6666") + 
  ylim(-0.05, 0.27)+
  scale_x_discrete(breaks = c(1,2,3,4),
                   labels = c("< 1 Yr","< 5 Yrs","< 10 Yrs", "> 10 Yrs"))+
  labs(x = element_blank(),
       y = element_blank(),
       title = "Co-Partisanship Effect\nin Tenure Subgroups") +
  theme_bw() -> tenure_party_plot
tenure_party_plot + 
  theme(axis.text.y = element_text(size=12),
        axis.text.x = element_text(size=14),
        legend.position = "none",
        plot.title = element_textbox(
          hjust = 0.5, margin = margin(t = 5, b = 5), size = 16)) -> tenure_party_plot

tenure_party_plot

#####extremism&party####
conditional.extreme<- cj(df.party, choice ~ sameparty, 
                         id = ~ IndID, estimate = "amce",
                         by = ~ ideo_extreme)
df.conditional.extreme <- extract_marginal(conditional.extreme)
print(df.conditional.extreme)

df.conditional.extreme$BY <- factor(df.conditional.extreme$BY, 
                                    levels = c("Moderate", 
                                               "Weak Ideology", 
                                               "Strong Ideology"))

ggplot(df.conditional.extreme, aes(x=BY, y=estimate)) + 
  geom_errorbar(aes(ymin=lower, ymax=upper), width=.1, position=position_dodge(.3),color="#9999CC") +
  geom_point(size=5, position=position_dodge(.3), color="#9999CC") +  
  geom_hline(yintercept = 0, color="#CC6666") + 
  ylim(-0.05, 0.27)+
  labs(x = element_blank(),
       y = element_blank(),
       title = "Co-Partisanship Effect\nin Ideology Intensity Subgroups") +
  theme_bw() -> extreme_party_plot
extreme_party_plot + 
  theme(axis.text.y = element_text(size=12),
        axis.text.x = element_text(size=14),
        legend.position = "none",
        plot.title = element_textbox(
          hjust = 0.5, margin = margin(t = 5, b = 5), size = 16)) -> extreme_party_plot

extreme_party_plot

#####Combine####
figure4<-ggarrange(ideo_party_plot,
                   extreme_party_plot+ theme(axis.text.y = element_blank(),
                                             axis.ticks.y = element_blank()),
                   elected_party_plot,
                   tenure_party_plot+ theme(axis.text.y = element_blank(),
                                            axis.ticks.y = element_blank()),
                   ncol = 2, nrow = 2)

figure4<-plot_grid(figure4)
figure4
ggsave(file = "figure4.eps", width = 10, height = 6, dpi = 666)
ggsave(file = "figure4.jpg", width = 10, height = 6, dpi = 666)

####Appendix D####
# Subgroup Analysis
#####Gender####
group_names_gender <- c("Female", "Other")
gender.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
                id = ~ IndID, estimate = "mm", by=~female)

gender.mm.plot<-plot(gender.mm, group="female", size = 3, vline = 0.5) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_color_manual("female",
                     breaks = group_names_gender,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
gender.mm.plot<-gender.mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                     legend.position="bottom",
                                     legend.title = element_blank(),
                                     legend.text = element_text(size = 12),
                                     strip.text = element_text(size = 14),
                                     axis.text.x = element_text(size = 12),
                                     axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top") +
  guides(colour = guide_legend(reverse=T))
gender.mm.plot

ggsave(file = "mm_gender.eps", width = 7, height = 7, dpi = 666)
ggsave(file = "mm_gender.jpg", width = 7, height = 7, dpi = 666)

#####Race####
group_names_race <- c("White", "Nonwhite")
race.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
              id = ~ IndID, estimate = "mm", by=~white_respond)

race.mm.plot<-plot(race.mm, group="white_respond", size = 3, vline = 0.5) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_color_manual("white_respond",
                     breaks = group_names_race,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
race.mm.plot<-race.mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                 legend.position="bottom",
                                 legend.title = element_blank(),
                                 legend.text = element_text(size = 12),
                                 strip.text = element_text(size = 14),
                                 axis.text.x = element_text(size = 12),
                                 axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
race.mm.plot
ggsave(file = "mm_race.eps", width = 7, height = 7, dpi = 666)
ggsave(file = "mm_race.jpg", width = 7, height = 7, dpi = 666)

#####Education####
group_names_education <- c("Graduate Degree", "Other")
education.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
                   id = ~ IndID, estimate = "mm", by=~grad)

education.mm.plot<-plot(education.mm, group="grad", size = 3, vline = 0.5) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_color_manual("grad",
                     breaks = group_names_education,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
education.mm.plot<-education.mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                           legend.position="bottom",
                                           legend.title = element_blank(),
                                           legend.text = element_text(size = 12),
                                           strip.text = element_text(size = 14),
                                           axis.text.x = element_text(size = 12),
                                           axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
education.mm.plot
ggsave(file = "mm_education.eps", width = 7, height = 7, dpi = 666)
ggsave(file = "mm_education.jpg", width = 7, height = 7, dpi = 666)

#####Age####
group_names_age <- c("Above 60", "Below 60")
age.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
             id = ~ IndID, estimate = "mm", by=~age1)

age.mm.plot<-plot(age.mm, group="age1", size = 3, vline = 0.5) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_color_manual("age1",
                     breaks = group_names_age,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
age.mm.plot<-age.mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                               legend.position="bottom",
                               legend.title = element_blank(),
                               legend.text = element_text(size = 12),
                               strip.text = element_text(size = 14),
                               axis.text.x = element_text(size = 12),
                               axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
age.mm.plot
ggsave(file = "mm_age.eps", width = 7, height = 7, dpi = 666)
ggsave(file = "mm_age.jpg", width = 7, height = 7, dpi = 666)

#####Population####
group_names_population <- c("Above 100k","Below 100k")
population.mm <- cj(df.party, choice ~ sameparty + resource + trust + outcome, 
                    id = ~ IndID, estimate = "mm", by=~pop1)

population.mm.plot<-plot(population.mm, group="pop1", size = 3, vline = 0.5) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_color_manual("pop1",
                     breaks = group_names_population,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
population.mm.plot<-population.mm.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                             legend.position="bottom",
                                             legend.title = element_blank(),
                                             legend.text = element_text(size = 12),
                                             strip.text = element_text(size = 14),
                                             axis.text.x = element_text(size = 12),
                                             axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
population.mm.plot
ggsave(file = "mm_population.eps", width = 7, height = 7, dpi = 666)
ggsave(file = "mm_population.jpg", width = 7, height = 7, dpi = 666)

####Appendix E####
# Estimated Difference in Marginal Means
######Overall####
conditional.diff<- cj(df.party, choice ~ resource + trust + outcome, 
                      id = ~ IndID, estimate = "mm_diff",
                      by = ~ sameparty)

conditional.diff.plot<-plot(conditional.diff, group="sameparty", size = 3, vline = 0,
                            feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(-0.1,0.3), breaks = c(-0.1,0,0.1,0.2,0.3))+
  labs(title="Overall",
       x = element_blank()) +
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
conditional.diff.plot<-conditional.diff.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                                   legend.position="none",
                                                   strip.text = element_text(size = 14),
                                                   legend.text = element_text(size = 14),
                                                   legend.background = element_blank(),
                                                   axis.text.y=element_text(size = 12),
                                                   axis.text.x = element_text(size = 12),
                                                   axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.diff.plot

######Democrat####
conditional.d.diff<- cj(df.dem, choice ~ resource + trust + outcome, 
                        id = ~ IndID, estimate = "mm_diff",
                        by = ~ sameparty)

conditional.d.diff.plot<-plot(conditional.d.diff, group="sameparty", size = 3, vline = 0,
                              feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(-0.1,0.3), breaks = c(-0.1,0,0.1,0.2,0.3))+
  labs(title="Democrat Respondents",
       x = element_blank()) +
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
conditional.d.diff.plot<-conditional.d.diff.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                                       legend.position="none",
                                                       strip.text = element_text(size = 14),
                                                       legend.text = element_text(size = 14),
                                                       legend.background = element_blank(),
                                                       axis.text.x = element_text(size = 12),
                                                       axis.title.x = element_text(size = 14),
                                                       axis.text.y = element_blank(),
                                                       axis.ticks.y = element_blank()) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.d.diff.plot

######Republican####
conditional.r.diff<- cj(df.rep, choice ~ resource + trust + outcome, 
                        id = ~ IndID, estimate = "mm_diff",
                        by = ~ sameparty)

conditional.r.diff.plot<-plot(conditional.r.diff, group="sameparty", size = 3, vline = 0,
                              feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(-0.1,0.3), breaks = c(-0.1,0,0.1,0.2,0.3))+
  labs(title="Republican Respondents",
       x = element_blank()) +
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names,
                     values = c("#9999CC", "#CC6666"))+
  theme_bw()
conditional.r.diff.plot<-conditional.r.diff.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                                       legend.position="none",
                                                       strip.text = element_text(size = 14),
                                                       legend.text = element_text(size = 14),
                                                       legend.background = element_blank(),
                                                       axis.text.x = element_text(size = 12),
                                                       axis.title.x = element_text(size = 14),
                                                       axis.text.y = element_blank(),
                                                       axis.ticks.y = element_blank()) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.r.diff.plot

######Combine####
figuree <- list(conditional.diff.plot, conditional.d.diff.plot, conditional.r.diff.plot)
figuree <- wrap_plots(figuree, ncol = 3, nrow = 1)
figuree <- figuree + plot_annotation(caption = "Estimated Difference (Ref. = Different Party)",
                                     theme = theme(plot.caption = element_text(hjust = 0.62, face = "plain", size = 14)))
figuree
ggsave(file = "figuree.eps", width = 9.5, height = 7, dpi = 666)
ggsave(file = "figuree.jpg", width = 9.5, height = 7, dpi = 666)

####Appendix F####
# Independents
label(df.ind$partyid) <- "Program Proposed By"

#####MM####
main.mm.ind <- cj(df.ind, choice ~ partyid + resource + trust + outcome, 
                  id = ~ IndID, estimate = "mm")
main.mm.ind

mm.ind.plot<-plot(main.mm.ind, size = 3, vline = 0.5,
                  feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.19,0.81), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  scale_color_manual(values = c("black","black","black","black"))+
  theme_bw()
mm.ind.plot<-mm.ind.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                               legend.position = "none",
                               strip.text = element_text(size = 14),
                               axis.text.y=element_text(size = 12),
                               axis.text.x = element_text(size = 12),
                               axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top") +
  guides(colour = guide_legend(reverse=T))
mm.ind.plot
ggsave(file = "figuref1.eps", width = 7, height = 7, dpi = 666)
ggsave(file = "figuref1.jpg", width = 7, height = 7, dpi = 666)

#####MM_Sub####
conditional.ind<- cj(df.ind, choice ~ resource + trust + outcome, 
                     id = ~ IndID, estimate = "mm", by = ~ partyid)

group_names_ind <- c("Democrats","Republicans")
conditional.ind.plot<-plot(conditional.ind, group="partyid", size = 3, vline = 0.5,
                           feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(0.19,0.81), breaks = c(0.2,0.3,0.4,0.5,0.6,0.7,0.8))+
  guides(color=guide_legend(title="Program Proposed By")) +
  scale_color_manual("job",
                     breaks = group_names_ind,
                     values = c("blue", "firebrick1"))+
  theme_bw()
conditional.ind.plot<-conditional.ind.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                                 legend.direction = "vertical",
                                                 legend.position="bottom",
                                                 legend.title = element_text(size = 14),
                                                 legend.key = element_blank(),
                                                 strip.text = element_text(size = 14),
                                                 legend.text = element_text(size = 14),
                                                 legend.background = element_blank(),
                                                 axis.text.y=element_text(size = 12),
                                                 axis.text.x = element_text(size = 12),
                                                 axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.ind.plot

#####MM_diff####
conditional.ind.diff<- cj(df.ind, choice ~ resource + trust + outcome, 
                          id = ~ IndID, estimate = "mm_diff",
                          by = ~ partyid)

conditional.ind.diff.plot<-plot(conditional.ind.diff, group="partyid", size = 3, vline = 0,
                                feature_headers = FALSE) + 
  geom_errorbarh(aes_string(xmin = "lower", xmax = "upper"),
                 size=0.5,height = 0,
                 position = ggstance::position_dodgev(height = 0.75))+
  scale_x_continuous(limits = c(-0.21,0.21), breaks = c(-0.2,-0.1,0,0.1,0.2))+
  guides(color=guide_legend(title="Program Proposed By")) +
  labs(x = "Estimated Difference (Ref. = Democrats)") +
  scale_color_manual("job",
                     breaks = group_names_ind,
                     values = c("blue", "firebrick1"))+
  theme_bw()
conditional.ind.diff.plot<-conditional.ind.diff.plot+theme(plot.title = element_text(hjust = 0.5,size = 14),
                                                           legend.position="none",
                                                           strip.text = element_text(size = 14),
                                                           legend.text = element_text(size = 14),
                                                           legend.background = element_blank(),
                                                           axis.text.y=element_blank(),
                                                           axis.ticks.y = element_blank(),
                                                           axis.text.x = element_text(size = 12),
                                                           axis.title.x = element_text(size = 14)) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      labeller = labeller(feature=feature.labs),
                      scales = "free_y", strip.position = "top")
conditional.ind.diff.plot

#####Combine####
figuref2<-list(conditional.ind.plot,conditional.ind.diff.plot)
figuref2<-wrap_plots(figuref2,ncol = 2, nrow = 1)
figuref2
ggsave(file = "figuref2.eps", width = 9, height = 8, dpi = 666)
ggsave(file = "figuref2.jpg", width = 9, height = 8, dpi = 666)


